var paginationTotal = 1;//记录总数
$(function(){
	$('[name=receiving_status]').val(1);	//	初始化状态为 1
	initData(0); //初始化分页数据
    $('#dateFrom,#dateTo').datepicker({
		changeMonth: true,
		changeYear: true,			 
		dateFormat: 'yy-mm-dd'
	});
	
	$("#printDialog").dialog({
		autoOpen:false,
		modal:true,
		width:500,
		show:"slide",
		buttons:{
			"OK":function(){
				$("#printForm").submit();
			},
			"Close":function(){
				$(this).dialog("close");
			}
		}
	});
	
	$("#tipDialog").dialog({
		autoOpen:false,
		modal:true,
		width:500,
		show:"slide",
		buttons:{
			"Close":function(){
				$(this).dialog("close");
			}
		}
	});
	
	$('.toggleButton').click(function(){
		if($(this).attr("status") != 1){
			$(".detailInfo").hide();
			$(this).attr("status",1);
			$(this).html("Unfold");
		}else{
			$(".detailInfo").show();
			$(this).attr("status",0);
			$(this).html("Fold");
		}
	});
	
	$("#right_confirm").dialog({
		autoOpen:false,
		modal:true,
		height:160,
		show:"slide",
		buttons: {
			'OK': function() {
				loading();
				$.ajax({
					type: "post",
					async: false,
					dataType: "json",
					url: "/merchant/returns/delete", 
					data: "id=" + Uid,
					success: function(json) { 
						if(json.ask=='1'){
							initData(0);							
							$("#tipDialog").html("Delete OK.");
							
						}else{
							$("#tipDialog").html("Delete Failed.");;
						}
					}
				});
				$(this).dialog('close');
//				$("#tipDialog").dialog("open");
			},
	    	'Cancel': function() {
					$(this).dialog('close');
			 }
		}

	});
	
});

function loadData(page,pageSize){
    var receiving_code = $('[name=receiving_code]').val();	//ASN No
    var ref_id = $('[name=ref_id]').val();				//Reference No
    var status = $('[name=receiving_status]').val();
    var dateFrom = $('[name=dateFrom]').val();
    var dateTo = $('[name=dateTo]').val();
	$.ajax({
		type: "POST",
		async: false,
		dataType: "json",
		url: "/merchant/returns/list",
		data:{
			'page':page,'pageSize':pageSize,
            'receiving_code':receiving_code,'status':status,'dateFrom':dateFrom,'dateTo':dateTo,
            'ref_id':ref_id
		},
		success: function(json) {
			var headerHtml = '';		//不同状态不同的列表头
			if(status == 0 || status == 1){
				headerHtml += '<div class="cell" style="width:17%">RMA code</div>';
				headerHtml += '<div class=" cell" style="width:18%">ReferenceNo</div>';
				headerHtml += '<div class="cell" style="width:15%">CreateDate</div>';
				
				headerHtml += '<div class="cell" style="width:8%">TotalQty</div>';
				headerHtml += '<div class="cell" style="width:20%">Instractions</div>';
				headerHtml += '<div class="cell" style="width:10%">Action</div>';
				headerHtml += '<div class="cell" style="width:12%">Result</div>';
			}
			
			if(status == 3){
				headerHtml += '<div class="cell" style="width:20%">RMA Code</div>';
				headerHtml += '<div class=" cell" style="width:20%">ReferenceNo</div>';				
				headerHtml += '<div class="cell" style="width:8%">TotalQty</div>';				
				headerHtml += '<div class="cell" style="width:20%">ReceivedQty</div>';
				headerHtml += '<div class="cell" style="width:20%">Type</div>';
				headerHtml += '<div class="cell" style="width:12%">Result</div>';
			}
			
			$(".row-header").html(headerHtml);
			var html = "";
			if(json.ask != 1) {
				paginationTotal = 0;
				html = '<div class="line1">&nbsp;No data returns</div>';
			}else {
                paginationTotal = json.total;
                $.each(json.result,function(key,val){
					html += '<div class="row height25">';					
					if(status == 0 || status == 1 ){						
						html += '<div class="cell orderCode" style="width:17%" receivingId="'+val.receiving_id+'">'+val.receiving_code+'</div>';
						html += '<div class="cell " style="width:18%" title="'+val.ref_id+'">'+val.ref_id+'</div>';
						html += '<div class="cell " style="width:15%" title="'+val.created+'">'+val.created+'</div>';
						html += '<div class="cell " style="width:8%" title="'+val.totalQty+'">'+val.totalQty+'</div>';
						html += '<div class="cell " style="width:20%" title="'+val.description+'">'+val.description+'</div>';
						html += '<div class="cell" style="width:10%;padding-top:4px;">';
														
							if(status == 1 ){
								html += '<a href="/merchant/returns/edit/id/'+val.receiving_id+'"><img src="/images/icon_edit.gif" class="editIcon" /></a>';
								html += '<img class="cursor" onclick="receivingDel(\''+val.receiving_code+'\')" title="Delete" src="/images/icon_del.gif">';
							}							
						html += '</div>';
						html += '<div class="cell no-background" style="width:12%" title="">'+val.returns_result+'</div>';
						html += '</div>';
					}

					if(status == 3){
						html += '<div class="cell orderCode" style="width:20%" receivingId="'+val.receiving_id+'">'+val.receiving_code+'</div>';
						html += '<div class="cell " style="width:20%" title="'+val.ref_id+'">'+val.ref_id+'</div>';
						
						html += '<div class="cell " style="width:8%" title="'+val.totalQty+'">'+val.totalQty+'</div>';
						html += '<div class="cell " style="width:20%" title="'+val.received_qty+'">'+val.received_qty+'</div>';	
						html += '<div class="cell " style="width:20%" title="">'+val.order_type+'</div>';
						html += '<div class="cell " style="width:12%" title="">'+val.returns_result+'</div>';
						html += '</div>';
					}
					
					// 每次 的产品具体信息
                	html += '<div style="width:100%;border-bottom:1px solid #cccccc;display:none;font-style:italic;background-color:#CAD9EA;" id="detailInfo'+val.receiving_id+'" class="detailInfo">';	
                	html += '<div class="row height25 bold">';
                	html += '<div class=" cell" style="width:13%">SKUNo</div>';
					html += '<div class="cell" style="width:10%">SKUName</div>';					
					html += '<div class="cell" style="width:10%">Weight</div>';
					html += '<div class="cell" style="width:10%">OrderQty</div>';

					html += '<div class="cell" style="width:12%">ReceivedQty</div>';					
					html += '<div class="cell" style="width:15%">Non-LabeledQty</div>';
					html += '<div class="cell" style="width:12%">IsPackaged</div>';
					html += '<div class="cell" style="width:18%">Remark</div>';					
					html += '</div>';
					
					$.each(val.products,function(pK,pV){
						html += '<div class="row height25">';
						html += '<div class="cell" style="width:13%">'+pV.SKUNo+'</div>';
						html += '<div class="cell" style="width:10%">'+pV.SKUName+'</div>';
						html += '<div class=" cell" style="width:10%">'+pV.weight+'</div>';
						html += '<div class=" cell" style="width:10%">'+pV.order_qty+'</div>';
						
						html += '<div class="cell" style="width:12%">'+pV.received_qty+'</div>';
						html += '<div class="cell" style="width:15%">'+pV.non_labeled_qty+'</div>';
						html += '<div class=" cell" style="width:12%">'+pV.packaged+'</div>';
						html += '<div class=" cell" style="width:18%">'+pV.remark+'</div>';
						html += '</div>';
						
					});
					html += '</div>';
                });
			}
			$("#receiving-list").html(html);	
			$(".orderCode").click(function(){
				$("#detailInfo"+$(this).attr("receivingId")).toggle();
			});
			$("#status0").html(json.statusTotal.deleteTotal);
			$("#status1").html(json.statusTotal.awaitingTotal);
			$("#status3").html(json.statusTotal.receivedTotal);
		}	//success
	});
	
}

function search() {
   initData(0);
}

//删除
function receivingDel(id){
	if(id==''||id==undefined){
		return false;
	}
	Uid = id;
	$('#right_confirm').dialog('open');
}

function clear(){
	$('[name=receiving_code]').val('');	//ASN No
    $('[name=ref_id]').val('');				//Reference No
    $('[name=receiving_status]').val('');
    $('[name=dateFrom]').val('');
    $('[name=dateTo]').val('');
}